package com.ap.core.dao.impl;

import com.ap.core.dao.AbstractAPCoreBaseJpaDao;
import com.ap.core.dao.BasketReferenceDao;
import com.ap.core.domain.BasketReference;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.LocalDateTime;

/**
 * Jpa Implementation class for {@link com.ap.core.dao.BasketReferenceDao}
 */
public class JpaBasketReferenceDao extends AbstractAPCoreBaseJpaDao<BasketReference, Long> implements BasketReferenceDao {

    @Override
    public BasketReference findById(Long primaryKey) {
        return findById(BasketReference.class, primaryKey);
    }
    
    @Override
	public BasketReference getReadOnlyBasketReferenceByBRN(String basketReferenceNumber){
        Criteria criteria = getSession().createCriteria(BasketReference.class);
        criteria.add(Restrictions.eq("basketReferenceNumber", basketReferenceNumber));
        criteria.addOrder(Order.desc("expiryTimeStamp"));
        criteria.setReadOnly(true);
        criteria.setMaxResults(1);
        return (BasketReference)criteria.uniqueResult();
	}

    @Override
    public void updateBRNRetrieved(String apTransactionId) {
        javax.persistence.Query query = entityManager.createNamedQuery("updateBRNRetrieved");
        query.setParameter("apTransactionId", apTransactionId);
        query.setParameter("retrievalTimeStamp", new LocalDateTime());
        query.executeUpdate();
    }

}
